<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/31 0031
 * Time: 13:41
 */


namespace Admin\Controller;

use Think\Page;
use Think\Think;
use Think\Upload;

class RewardController extends BaseController
{
    public function detail()
    {


        $param = I('get.');
        if ($param['start_time'] && !$param['end_time']) {
            $where['f.create_time'] = array('gt', strtotime($param['start_time']));
        }
        if ($param['end_time'] && !$param['start_time']) {
            $where['f.create_time'] = array('lt', strtotime($param['end_time']));
        }

        if ($param['start_time'] && $param['end_time']) {
            $where['f.create_time'] = array(array('gt', strtotime($param['start_time'])), array('lt', strtotime($param['end_time'])));

        }
        if($param['username']){
            $where['u.username'] = $param['username'];
            $this->assign('username',$param['username']);
        }

        if ($param['coin']){
            if ($param['coin']==1){
                $where['f.type']=array('in','1,7,9,10');
            }
            if ($param['coin']==8){
                $where['f.type']=8;
            }
            $this->assign('coin',$param['coin']);
        }

        if($param['reward_type']){
            if($param['reward_type']==1){
                $where['f.reward_type']=array('in','199,899');
            }
            if($param['reward_type']==2){
                $where['f.reward_type']=555;
            }
            if($param['reward_type']==3){
                $where['f.reward_type']=array('in','101,801');
            }
            if($param['reward_type']==4){
                $where['f.type']=7;
                $where['f.reward_type']=221;
            }
            if($param['reward_type']==5){
                $where['f.type']=7;
                $where['f.reward_type']=222;
            }
            if($param['reward_type']==6){//等级收益
                $where['f.type']=9;
                $where['f.reward_type']=222;
            }
            if($param['reward_type']==7){//节点，全球收益
                $where['f.type']=10;
                $where['f.reward_type']=222;
            }
            $this->assign('reward_type',$param['reward_type']);
        }
//        dump($where);die;

        $fmoney = M('fmoney');
        $join = 'left join '.C('DB_PREFIX').'user as u on u.id = f.user_id';
        $count = $fmoney->alias('f')->join($join)->order('f.create_time desc')->where($where)->count();
        $this->assign('count',$count);
        $Page = new Page($count,20);
        $list = $fmoney->alias('f')->join($join)->order('f.create_time desc')->where($where)->limit($Page->firstRow.','.$Page->listRows)->field('f.*,u.username')->select();
        $user = M('user');
        foreach ($list as $k=>$v){
            $data = $user->field('username')->find($v['from_id']);
            $list[$k]['from_id'] = $data['username'];
        }
        $this->assign('page',$Page->show());
        $this->assign('list',$list);
        $this->display();
    }
    /**
     * 奖金明细导出
     * User: ming
     * Date: 2020/4/29 15:43
     */
    public function export_detail()
    {
        $param = I('get.');
        if ($param['start_time'] && !$param['end_time']) {
            $where['f.create_time'] = array('gt', strtotime($param['start_time']));
        }
        if ($param['end_time'] && !$param['start_time']) {
            $where['f.create_time'] = array('lt', strtotime($param['end_time']));
        }

        if ($param['start_time'] && $param['end_time']) {
            $where['f.create_time'] = array(array('gt', strtotime($param['start_time'])), array('lt', strtotime($param['end_time'])));

        }
        if($param['username']){
            $where['u.username'] = $param['username'];
            $this->assign('username',$param['username']);
        }

        if ($param['coin']){
            if ($param['coin']==1){
                $where['f.type']=array('in','1,7,9,10');
            }
            if ($param['coin']==8){
                $where['f.type']=8;
            }
            $this->assign('coin',$param['coin']);
        }

        if($param['reward_type']){
            if($param['reward_type']==1){
                $where['f.reward_type']=array('in','199,899');
            }
            if($param['reward_type']==2){
                $where['f.reward_type']=555;
            }
            if($param['reward_type']==3){
                $where['f.reward_type']=array('in','101,801');
            }
            if($param['reward_type']==4){
                $where['f.type']=7;
                $where['f.reward_type']=221;
            }
            if($param['reward_type']==5){
                $where['f.type']=7;
                $where['f.reward_type']=222;
            }
            if($param['reward_type']==6){//等级收益
                $where['f.type']=9;
                $where['f.reward_type']=222;
            }
            if($param['reward_type']==7){//节点，全球收益
                $where['f.type']=10;
                $where['f.reward_type']=222;
            }
            $this->assign('reward_type',$param['reward_type']);
        }

        $fmoney = M('fmoney');
        $join = 'left join '.C('DB_PREFIX').'user as u on u.id = f.user_id';
        $list = $fmoney->alias('f')->join($join)->order('f.create_time desc')->where($where)->field('f.*,u.username')->select();
        $user = M('user');
        $type = C('account_type');
        foreach ($list as $k=>$v){
            $data = $user->field('username')->find($v['from_id']);
            $list[$k]['from_id'] = $data['username'];
            $list[$k]['coinname'] = $type[$v['type']];
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }


        $xlsName = '奖金明细';
        $xlsCell = array(
            ['username','用户名'],
            ['from_id','提供人'],
            ['coinname','类型'],
            ['amount','数量'],
            ['tips','备注'],
            ['create_time','时间'],

        );


        if($list){
            exportExcel($xlsName,$xlsCell,$list);
        }else{
            $this->error('找不到数据');
        }





    }

    //一元购活动
    public function start_lottery(){

        //筛选条件

        //查询数据
        $count=M('lottery')->count();
        $page=new Page($count,20);
        $show=$page->show();
        $data=M('lottery')->order('lottery_id desc')->limit($page->firstRow.','.$page->listRows)->select();
        foreach ($data as $k=>$v){
            if($v['lottery_time']){

            }else{
                $data[$k]['lottery_time']='';
            }

        }
        $this->assign('count',$count);
        $this->assign('data',$data);
        $this->assign('show',$show);
        $this->display();
    }

    //新增一元购
    public function add_lottery(){

        $id=I('get.id',0);
            if($id>0){
                $lottery=M('lottery')->where(array('id'=>I('get.id')))->find();
                $this->assign('lottery',$lottery);
//                dump($lottery);die;
            }else{
                $lottery=M('lottery')->max('lottery_id');
                if($lottery){
                    $lottery_id=$lottery+1;
                }else{
                    $lottery_id=1;
                }
                $this->assign('lottery_id',$lottery_id);
            }
            $this->assign('id',$id);

            $this->display();

    }

    public function new_lottery(){
        if(IS_POST){
            $data=I('post.');
//            dump($data);die;
            $start=strtotime($data['start_time']);
            $end=strtotime($data['end_time']);
//            dump($data);die;
            if($data['mark_number']&&$data['award']&&$data['award_price']&&$start&&$end){
                if($start>$end){
                    ajax_return(0,'时间不合理');
                }else{
                    $data['start_time']=$start;
                    $data['end_time']=$end+86399;
                    if($data['start_time']<=time() && $data['end_time']>time()){
                        $data['status']=1;//活动中
                    }elseif($data['start_time']>time()){
                        $data['status']=2;//未开始
                    }else{
                        $data['status']=0;//已结束
                    }
                    $data['award_img']=$data['logo'];
                    $data['award_detail']=$data['detail'];
                    if(M('lottery')->where(array('lottery_id'=>$data['lottery_id']))->find()){
                        $res=M('lottery')->where(array('lottery_id'=>$data['lottery_id']))->save($data);
                    }else{
                        $res=M('lottery')->add($data);
                    }
                    if($res){
                        ajax_return(1,'新增成功',U('start_lottery'));
                    }else{
                        ajax_return(0,'新增失败');
                    }
                }
            }else{
                ajax_return(0,'信息不完整');
            }

        }
    }

    public function logo_change(){
        $upload=new Upload();
        $upload->savePath='goods/';
        $upload->exts=array('jpg','png','bmp','jpeg');
        $info=$upload->upload();
        if($info){
            $this->ajaxReturn(array('code'=>1,'path'=>'/Uploads/'.$info['file']['savepath'].$info['file']['savename']));
        }else{
            $this->ajaxReturn(array('code'=>0,'message'=>$upload->getError()));
        }
    }

    //z指定中奖用户
    public function fixed(){
        $data=I('post.');
        if(!M('user')->where(array('username'=>$data['username']))->find()){
            $this->ajaxReturn(array('code'=>0,'message'=>'系统不存在该用户'));
        }
        $id=M('user')->where(array('username'=>$data['username']))->getField('id');
        if(!M('lottery_user')->where(array('lottery_id'=>$data['lottery_id'],'uid'=>$id))->select()){
            $this->ajaxReturn(array('code'=>0,'message'=>'该用户未参与过本期抽奖'));
        }
        if(M('lottery_fixed')->where(array('lottery_id'=>$data['lottery_id']))->find()){
            $this->ajaxReturn(array('code'=>0,'message'=>'本期已经有了指定的中奖用户'));
        }
        $lottery_fixed['uid']=$id;
        $lottery_fixed['lottery_id']=$data['lottery_id'];
        $lottery_fixed['is_fixed']=1;
        $lottery_fixed['time']=time();
        M('lottery_fixed')->add($lottery_fixed);
        $this->ajaxReturn(array('code'=>1,'message'=>'操作成功。指定了'.$data['username'].'为该期中奖用户'));
    }

    //开奖按钮功能
    public function open_lottery(){
        $lottery_id=I('post.lottery_id');
        $lottery=M('lottery')->where(array('lottery_id'=>$lottery_id))->find();//该期信息
        $user_arr=M('lottery_user')->where(array('lottery_id'=>$lottery_id))->getField('uid',true);
        if($user_arr){
            //查看是否指定中奖
            if($lottery_fixed=M('lottery_fixed')->where(array('lottery_id'=>$lottery['lottery_id']))->find()){
                $winner=$lottery_fixed['uid'];
            }else{
                $user_arr=M('lottery_user')->where(array('lottery_id'=>$lottery['lottery_id']))->getField('uid',true);
                $winner=$user_arr[array_rand($user_arr)];//随机取一个uid用户
            }

//            $winner=$user_arr[array_rand($user_arr)];//中奖者

            $res1=M('lottery')->where(array('lottery_id'=>$lottery_id))->save(array('status'=>0,'flag'=>1));//抽奖 表状态改为0（结束）
            $lottery_winner['uid']=$winner;
            $lottery_winner['lottery_id']=$lottery_id;
            $lottery_winner['award_price']=$lottery['award_price'];
            $lottery_winner['award']=$lottery['award'];
            $lottery_winner['number']=M('lottery_user')->where(array('lottery_id'=>$lottery_id,'uid'=>$winner))->sum('number');//中奖者已投多少标
            $res2=M('lottery_winner')->add($lottery_winner);//更新中奖者表
            $res3=M('lottery')->where(array('lottery_id'=>$lottery_id))->save(array('lottery_time'=>time()));
            if($res1&&$res2&&$res3){
                $this->add_fmoney($this->admin['id'],$this->admin['id'],1,8,810,'管理员手动强行开奖');
                $news['title']='第'.$lottery_id.'期抽奖';
                $news['content']='恭喜获得'.$lottery['award'].'奖品';
                $news['type']=2;//1为平台公告2为系统公告
                $news['create_time']=time();
                $news['is_lottery']=1;
                $news['uid']=$winner;
                M('news')->add($news);//中奖者发布消息

//            $this->ajaxReturn(array('code'=>1,'message'=>'成功'));
                ajax_return(1,'操作成功');
            }else{
                ajax_return(0,'操作失败');
            }
        }else{
            ajax_return(0,'还没有投注的人');
        }

    }


    //补标
    public function bubiao(){
        $data=I('post.');
//        dump($data);die;
        $username=I('post.username');
        $user_arr=M('user')->where('id<>1')->getField('username',true);
        if(in_array($username,$user_arr)){
            $lottery=M('lottery')->where(array('lottery_id'=>$data['lottery_id']))->find();
            $uid=M('user')->where(array('username'=>$username))->find();
            //插入补标的账户
            $lottery_user['uid']=$uid['id'];
            $lottery_user['lottery_id']=$data['lottery_id'];
            $lottery_user['user_address']=$uid['eth_address'];
            $lottery_user['number']=1;
            $lottery_user['amount']=$lottery['least_tou'];//投入数量
            $lottery_user['time']=time();
            M('lottery_user')->add($lottery_user);
            M('lottery')->where(array('lottery_id'=>$data['lottery_id']))->setInc('already_biao');//增加已投标数
            $this->add_fmoney($uid['id'],$this->admin['id'],1,8,812,'管理员手动补标');
            //查看是否达到满标开奖
            //查看这期已投标数与满标是否相等
            $lottery=M('lottery')->where(array('lottery_id'=>$data['lottery_id']))->find();
            if($lottery['already_biao']==$lottery['mark_number']){
                //满标开奖

                //查看是否指定中奖
                if($lottery_fixed=M('lottery_fixed')->where(array('lottery_id'=>$lottery['lottery_id']))->find()){
                    $winner=$lottery_fixed['uid'];
                }else{
                    $user_arr=M('lottery_user')->where(array('lottery_id'=>$data['lottery_id']))->getField('uid',true);
                    $winner=$user_arr[array_rand($user_arr)];//随机取一个uid用户
                }
//                $user_arr=M('lottery_user')->where(array('lottery_id'=>$data['lottery_id']))->getField('uid',true);
//                $winner=$user_arr[array_rand($user_arr)];//随机取一个uid用户
                //修改抽奖状态
                $res4=M('lottery')->where(array('lottery_id'=>$data['lottery_id']))->save(array('status'=>0,'flag'=>1,'lottery_time'=>time()));//抽奖 表状态改为0（结束）
                $lottery_winner['uid']=$winner;
                $lottery_winner['lottery_id']=$data['lottery_id'];
                $lottery_winner['award_price']=$lottery['award_price'];
                $lottery_winner['award']=$lottery['award'];
                $lottery_winner['number']=M('lottery_user')->where(array('lottery_id'=>$data['lottery_id'],'uid'=>$winner))->sum('number');//中奖者已投多少标
                M('lottery_winner')->add($lottery_winner);//更新中奖者表

                $news['title']='第'.$data['lottery_id'].'期抽奖';
                $news['content']='恭喜获得'.$lottery['award'].'奖品';
                $news['type']=2;//1为平台公告2为系统公告
                $news['create_time']=time();
                $news['is_lottery']=1;
                $news['uid']=$winner;
                M('news')->add($news);//中奖者发布消息
                $this->add_fmoney($this->admin['id'],$this->admin['id'],1,8,810,'系统自动满标开奖');
            }
            ajax_return(1,'补标操作成功');

        }else{
            ajax_return(0,'账户不存在');
        }
    }

    //流标
    public function liubiao(){
        $lottery_id=I('post.lottery_id');
        $user_arr=M('lottery_user')->where(array('lottery_id'=>$lottery_id))->field('uid,number')->select();
        if($user_arr){
            foreach ($user_arr as $k=>$v){
                if ($v['coin']==8){//usdt抽奖
                    $res1= M('user')->where(array('id'=>$v['uid']))->setInc('z8',$v['amount']);
                }elseif ($v['coin']==1){//exuu抽奖
                    $res1= M('user')->where(array('id'=>$v['uid']))->setInc('z1',$v['amount']);
                }
            }
            $res2=M('lottery')->where(array('lottery_id'=>$lottery_id))->save(array('status'=>3,'lottery_time'=>time()));
            $this->add_fmoney($this->admin['id'],$this->admin['id'],1,8,811,'管理员手动流标');
            if($res1&&$res2){
                ajax_return(1,'流标操作成功');
            }else{
                ajax_return(0,'流标操作失败');
            }
        }else{
            ajax_return(0,'还没有用户投注');
        }
    }

    //一元购活动管理
    public function lotterymanage(){
        //筛选条件

        //查询数据

        $count=M('lottery_winner')->count();
        $page=new Page($count,20);
        $show=$page->show();
        $winner_list=M('lottery_winner')->alias('lw')->join('ms_lottery l on l.lottery_id=lw.lottery_id')->where('lw.status=0')->field('lw.*,l.lottery_time,l.mark_number')->order('l.lottery_time desc')->limit($page->firstRow.','.$page->listRows)->select();
        foreach ($winner_list as $k=>$v){
            $user=M('user')->where(array('id'=>$v['uid']))->find();
            $winner_list[$k]['username']=$user['username'];
            $winner_list[$k]['address']=$user['eth_address'];
            $winner_list[$k]['lottery_name']=$user['lottery_name'];
            $winner_list[$k]['lottery_phone']=$user['lottery_phone'];
            $winner_list[$k]['lottery_address']=$user['lottery_address'];
        }
        $this->assign('data',$winner_list);
        $this->assign('count',$count);
        $this->assign('show',$show);
//        dump($winner_list);die;
        $this->display();
    }


    //奖金参数设置
    public function rewardConfig()
    {
        if(IS_POST){
            $param = I('post.');

           if($param['amount']){
               $l = M('level');
               foreach ($param['amount'] as $k => $v){
                   $map['id'] = $k+1;
                   $map['amount'] =  $v;
                    $l->save($map);
               }
           }
           $config=M('reward_config')->where('id=1')->find();
           if($param['ptr18']==$config['ptr18']){//未修改
               $flag=true;
           }

           $param['ptr38']=strtotime($param['ptr38']);//首存结束
           $param['ptr47']=strtotime($param['ptr47']);//存币赠送开始时间
           $param['ptr48']=strtotime($param['ptr48']);//结束时间
            if(M('reward_config')->save($param) === false){
                ajax_return(1,'保存失败');
            }
            if($flag){
                M()->execute('call auto_rate()');
            }


            M()->execute('call auto_shop()');

            ajax_return(0,'保存成功',U(''));
        }else{

            $this->assign('usdtapi',$this->usdtapi());
            $this->assign('data',M('reward_config')->find());
            $this->assign('datal',M('level')->select());
            $this->display();
        }
    }


    public function detailByUser()
    {
        $param = I('get.');
        if ($param['start_time'] && !$param['end_time']) {
            $param['start_time'] = substr($param['start_time'], 0, -1);
            $where['c.cha_date'] = array('gt', strtotime($param['start_time']));
        }
        if ($param['end_time'] && !$param['start_time']) {
            $param['end_time'] = substr($param['end_time'], 0, -1);
            $where['c.cha_date'] = array('lt', strtotime($param['end_time']));
        }

        if ($param['start_time'] && $param['end_time']) {
            $param['start_time'] = substr($param['start_time'], 0, -1);
            $param['end_time'] = substr($param['end_time'], 0, -1);
            $where['c.cha_date'] = array(array('gt', strtotime($param['start_time'])), array('lt', strtotime($param['end_time'])));

        }
        if($param['username']){
            $where['u.username'] = $param['username'];
            $this->assign('username',$param['username']);
        }

        $join = 'left join '.C('DB_PREFIX').'user as u on u.id = c.owner';
        $c = M('cha');
        $count = $c->alias('c')->join($join)->where($where)->order('c.cha_date desc')->group('c.owner')->field('count(*)')->select();
        $page = new Page(count($count),20);
        $data = $c->alias('c')->join($join)->where($where)->limit($page->firstRow.','.$page->listRows)->order('c.cha_date desc')->group('c.owner')->
        field('u.username,u.truename,sum(cha_001) as c1,sum(cha_002) as c2,sum(cha_013) as c13
        ')->select();
        $all = $c->alias('c')->join($join)->where($where)->field('sum(cha_001) as c1,sum(cha_002) as c2,sum(cha_013) as c13')->find();
        $this->assign('data',$data);
        $this->assign('all',$all);
        $this->assign('page',$page->show());
        $this->display();
    }

    /**
     * 商家申请记录
     * User: ming
     * Date: 2020/12/3 18:48
     */
    public function shopping_record(){
        $param = I('get.');
        if ($param['start_time'] && !$param['end_time']) {
            $where['create_time'] = array('gt', strtotime($param['start_time']));
        }
        if ($param['end_time'] && !$param['start_time']) {
            $where['create_time'] = array('lt', strtotime($param['end_time']));
        }

        if ($param['start_time'] && $param['end_time']) {
            $where['create_time'] = array(array('gt', strtotime($param['start_time'])), array('lt', strtotime($param['end_time'])));

        }
        if($param['username']){
            $check['username|phone'] = array('like','%'.$param['username'].'%');
            $userid = M('user')->where($check)->getField('id',true);
            $userid = empty($userid)?"":$userid;
            $where['user_id'] = array('in',$userid);
        }
        if ($param['reward_type']){
            $where['reward_type'] = $param['reward_type'];
        }else{
            $where['reward_type'] = array('in',[13]);
        }
        $where['type'] = 17;
        $this->assign('param',$param);
//        $where['type'] = 7;

        $count=M('fmoney')->where($where)->count();
        $page=new Page($count,20);
        $show=$page->show();
        $record=M('fmoney')->where($where)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        foreach ($record as $k=>$v){
            $user=M('user')->where(array('id'=>$v['user_id']))->find();
            $record[$k]['username']=$user['username'];
        }
        $this->assign('data',$record);
        $this->assign('count',$count);
        $this->assign('show',$show);
//        dump($winner_list);die;
        $this->display();
    }

    /**
     * 商家申请导出
     * User: ming
     * Date: 2020/12/3 18:50
     */
    public function exportShoppingRecord(){
        $param = I('get.');
        if ($param['start_time'] && !$param['end_time']) {
            $where['create_time'] = array('gt', strtotime($param['start_time']));
        }
        if ($param['end_time'] && !$param['start_time']) {
            $where['create_time'] = array('lt', strtotime($param['end_time']));
        }

        if ($param['start_time'] && $param['end_time']) {
            $where['create_time'] = array(array('gt', strtotime($param['start_time'])), array('lt', strtotime($param['end_time'])));

        }
        if($param['username']){
            $check['username|phone'] = array('like','%'.$param['username'].'%');
            $userid = M('user')->where($check)->getField('id',true);
            $userid = empty($userid)?"":$userid;
            $where['user_id'] = array('in',$userid);
        }
        if ($param['reward_type']){
            $where['reward_type'] = $param['reward_type'];
        }else{
            $where['reward_type'] = array('in',[13]);
        }
        $this->assign('param',$param);
        $where['type'] = 17;


        $record=M('fmoney')->where($where)->order('id desc')->select();
        foreach ($record as $k=>$v){
            $user=M('user')->where(array('id'=>$v['user_id']))->find();
            $record[$k]['username']=$user['username'];
            $record[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        $xlsName = '商家申请记录';
        $xlsCell = array(
            ['username','用户名'],
            ['amount','数量'],
            ['tips','备注'],
            ['create_time','时间'],
        );


        if($record){
            exportExcel($xlsName,$xlsCell,$record);
        }else{
            $this->error('找不到数据');
        }
    }

    /**
     * @Notes:划账记录
     * @author: Qian
     * @Time: 2020/11/25 9:07
     */
    public function lock_record(){
        $param = I('get.');
        if ($param['start_time'] && !$param['end_time']) {
            $where['create_time'] = array('gt', strtotime($param['start_time']));
        }
        if ($param['end_time'] && !$param['start_time']) {
            $where['create_time'] = array('lt', strtotime($param['end_time']));
        }

        if ($param['start_time'] && $param['end_time']) {
            $where['create_time'] = array(array('gt', strtotime($param['start_time'])), array('lt', strtotime($param['end_time'])));

        }
        if($param['username']){
            $check['username|phone'] = array('like','%'.$param['username'].'%');
            $userid = M('user')->where($check)->getField('id',true);
            $userid = empty($userid)?"":$userid;
            $where['user_id'] = array('in',$userid);
        }
        if ($param['reward_type']){
            $where['reward_type'] = $param['reward_type'];
        }else{
            $where['reward_type'] = array('in',[123,12]);
        }
        $this->assign('param',$param);
//        $where['type'] = 7;

        $count=M('fmoney')->where($where)->count();
        $page=new Page($count,20);
        $show=$page->show();
        $record=M('fmoney')->where($where)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        foreach ($record as $k=>$v){
            $user=M('user')->where(array('id'=>$v['user_id']))->find();
            $record[$k]['username']=$user['username'];
        }
        $this->assign('data',$record);
        $this->assign('count',$count);
        $this->assign('show',$show);
//        dump($winner_list);die;
        $this->display();
    }
    /**
     * @Notes://导出划转记录
     * @author: Qian
     * @Time: 2020/11/26 10:10
     */
    public function exportLockRecord(){
        $param = I('get.');
        if ($param['start_time'] && !$param['end_time']) {
            $where['create_time'] = array('gt', strtotime($param['start_time']));
        }
        if ($param['end_time'] && !$param['start_time']) {
            $where['create_time'] = array('lt', strtotime($param['end_time']));
        }

        if ($param['start_time'] && $param['end_time']) {
            $where['create_time'] = array(array('gt', strtotime($param['start_time'])), array('lt', strtotime($param['end_time'])));

        }
        if($param['username']){
            $check['username|phone'] = array('like','%'.$param['username'].'%');
            $userid = M('user')->where($check)->getField('id',true);
            $userid = empty($userid)?"":$userid;
            $where['user_id'] = array('in',$userid);
        }
        if ($param['reward_type']){
            $where['reward_type'] = $param['reward_type'];
        }else{
            $where['reward_type'] = array('in',[123,12]);
        }
        $this->assign('param',$param);
//        $where['type'] = 7;


        $record=M('fmoney')->where($where)->order('id desc')->select();
        foreach ($record as $k=>$v){
            $user=M('user')->where(array('id'=>$v['user_id']))->find();
            $record[$k]['username']=$user['username'];
            $record[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        $xlsName = '理财划转记录';
        $xlsCell = array(
            ['username','用户名'],
            ['amount','数量'],
            ['tips','备注'],
            ['create_time','时间'],
        );


        if($record){
            exportExcel($xlsName,$xlsCell,$record);
        }else{
            $this->error('找不到数据');
        }
    }

    //usdt汇率
    public function usdtapi(){
        $url = "https://api.jinse.com/v3/coin/detail?slugs=tether&_source=m";
        $btcData = $this->https_request($url);
        $btcData = json_decode($btcData, true);
        return $btcData;
    }

    function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }


}